package com.hq.zb.utils;

import com.hq.zb.entity.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取请求上下
 * @author huanghanrui
 * @date 2022-03-24
 */
@Component
public class ContextHolderUtils {
    /**
     * SpringMvc下获取request
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest request = null;
        if (RequestContextHolder.getRequestAttributes()!=null) {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            request = ((ServletRequestAttributes) attributes).getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            Map<String, String> headers = new HashMap<>();
            if (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                headers.put(name, request.getHeader(name));
            }
        }
        return request;
    }

    public static CurrentUser getCurrentUser() {
        HttpServletRequest request = null;
        CurrentUser currentUser = new CurrentUser();
        if (RequestContextHolder.getRequestAttributes()!=null) {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            request = ((ServletRequestAttributes) attributes).getRequest();
            Enumeration<String> headerNames = request.getHeaderNames();
            Map<String, String> headers = new HashMap<>();
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                String value = request.getHeader(key);
                headers.put(key, value);
            }
            //获取请求authorization
            String authorization = headers.get("authorization");
            if (StringUtils.isNotBlank(authorization)) {
                //获取用户名
                String userName = JWTUtil.getUsername(authorization);
                currentUser = RedisUtil.getBean(userName, CurrentUser.class);
            }
        }
        return currentUser;
    }


    /**
     * SpringMvc下获取session
     *
     * @return
     */
    public static HttpSession getSession() {
        HttpServletRequest request = getRequest();
        String tempSessionId = request.getParameter("sessionId");
        HttpSession session = request.getSession();
        String sessionId = session.getId();
        if(!StringUtils.isEmpty(tempSessionId) && !tempSessionId.equals(sessionId)){
            sessionId = tempSessionId;
            if(sessionMap.containsKey(sessionId)){
                session = sessionMap.get(sessionId);
            }
        }
        if(!sessionMap.containsKey(sessionId)){
            sessionMap.put(sessionId, session);
        }
        return session;
    }


    private static final Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();

    public static HttpSession getSession(String sessionId){
        HttpSession session = sessionMap.get(sessionId);
        return session == null ? getSession() : session;
    }

    public static void removeSession(String sessionId){
        if(sessionMap.containsKey(sessionId)){
            sessionMap.remove(sessionId);
        }
    }

}
